package com.csot.ry.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.csot.common.constant.HttpStatus;
import com.csot.common.core.domain.entity.SysUser;
import com.csot.common.utils.PageUtils;
import com.csot.common.utils.SecurityUtils;
import com.csot.config.Caches;
import com.csot.model.CitySegment;
import com.csot.model.DcnSegment;
import com.csot.ry.domain.TIdleRecord;
import com.csot.ry.util.CacheKitUtil;
import com.csot.ry.util.PageUtil;
import com.csot.ry.util.UserUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.csot.common.annotation.Log;
import com.csot.common.core.controller.BaseController;
import com.csot.common.core.domain.AjaxResult;
import com.csot.common.enums.BusinessType;
import com.csot.ry.domain.TIdleDcn;
import com.csot.ry.service.ITIdleDcnService;
import com.csot.common.utils.poi.ExcelUtil;
import com.csot.common.core.page.TableDataInfo;

/**
 * dcn稽核Controller
 *
 * @author csot
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/ipmanagement/idledcn")
public class TIdleDcnController extends BaseController {
    @Autowired
    private ITIdleDcnService tIdleDcnService;

    DcnSegment ds = new DcnSegment().dao();

    /**
     * 查询dcn稽核列表
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idledcn:list')")
    @GetMapping("/list")
    public TableDataInfo list(TIdleDcn tIdleDcn, HttpServletRequest request) {
        List<TIdleDcn> list;
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptid = user.getDeptId();
        Long pdeptid;
        if (deptid != 100) {
            pdeptid = user.getDept().getParentId();
            String city_e = CacheKitUtil.ip_dept.get(String.valueOf(pdeptid));
            tIdleDcn.setIpCity(city_e);
        }
//        String city_zh = Caches.getDeptName(String.valueOf(pdeptid));

        HttpSession hs = request.getSession(true);
        list = tIdleDcnService.selectTIdleDcnList(tIdleDcn);
        startPage();
        Page page = PageUtils.getLocalPage();
        PageInfo pi = PageUtil.startPage(list, page.getPageNum(), page.getPageSize());
        return getDataTable(pi.getList(), list.size());


    }

    /**
     * 导出dcn稽核列表
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idledcn:export')")
    @Log(title = "dcn稽核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TIdleDcn tIdleDcn, HttpServletRequest request) {

        List<TIdleDcn> list = null;
        HttpSession hs = request.getSession();
        if (Objects.nonNull(hs.getAttribute("idledcn"))) {
            list = (List<TIdleDcn>) hs.getAttribute("idledcn");
        } else {
            list = tIdleDcnService.selectTIdleDcnList(tIdleDcn);
        }

        ExcelUtil<TIdleDcn> util = new ExcelUtil<TIdleDcn>(TIdleDcn.class);
        util.exportExcel(response, list, "dcn稽核数据");
    }

    /**
     * 获取dcn稽核详细信息
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idledcn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tIdleDcnService.selectTIdleDcnById(id));
    }

    /**
     * 新增dcn稽核
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idledcn:add')")
    @Log(title = "dcn稽核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TIdleDcn tIdleDcn) {
        return toAjax(tIdleDcnService.insertTIdleDcn(tIdleDcn));
    }

    /**
     * 修改dcn稽核
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idledcn:edit')")
    @Log(title = "dcn稽核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TIdleDcn tIdleDcn) {
        return toAjax(tIdleDcnService.updateTIdleDcn(tIdleDcn));
    }

    /**
     * 删除dcn稽核
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idledcn:remove')")
    @Log(title = "dcn稽核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tIdleDcnService.deleteTIdleDcnByIds(ids));
    }


    protected TableDataInfo getDataTable(List<?> list, long total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }
}
